<?php
// +----------------------------------------------------------------------
// | my
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\UserType;
use think\Controller;
use org\Verify;

class Login extends Controller
{
    //登录页面
    public function index()
    {

        $user=model('admin/User')->info(3,'shopid');
           $cur_date = strtotime(date('Y-m-d',time()));
           $map['shopid']=array('eq',$user['shopid']);
           $map['create_time']=array('EGT',$cur_date);
           $pics=db('PrinterPic')->where($map)->limit(10)->select();
           $message="打印列表：（仅仅显示最后10条）";
           foreach ($pics as $key => $pic) {
             echo $message.="\n".$pic['id'].':'.$pic['nickname'].'-'.$pic['status'];
           }

       return $this->fetch('/login');
    }

    //登录操作
    public function doLogin()
    {
        $name = input("param.name");
        $password = input("param.password");
        $code = input("param.code");

        $result = $this->validate(compact('name', 'password', "code"), 'AdminValidate');
        if(true !== $result){
            return json(['code' => -5, 'data' => '', 'msg' => $result]);
        }

        // $verify = new Verify();
        // if (!$verify->check($code)) {
        //     return json(['code' => -4, 'data' => '', 'msg' => '验证码错误']);
        // }
        // 
        
        //根据aid来登录并获得相应的权限
        $names = explode('@', $name);
        $name = $names[0];
        $aid = 0;
        if (count($names) == 2) {
            $aid = $names[1];
            $aid_title=db('AdminAid')->where('id',$aid)->value('title');
           
        }

        $hasUser = db('admin_user')->where(array('name|mobile' => $name,'aid' => $aid))->find();
        if(empty($hasUser)){
            return json(['code' => -1, 'data' => '', 'msg' => '管理员不存在']);
        }

        if(md5(md5($password) . config('data_auth_key'))!= $hasUser['password']){
            return json(['code' => -2, 'data' => '', 'msg' => '密码错误']);
        }

        if(1 != $hasUser['status']){
            return json(['code' => -6, 'data' => '', 'msg' => '该账号被禁用']);
        }

        //获取该管理员的角色信息
        $info = model('role')->getRoleInfo($hasUser['role_id']);

        session('user_auth', $hasUser);
        session('role', $info['rolename']);  //角色名
        session('rule', $info['rule']);  //角色节点
        session('action', $info['action']);  //角色权限
        session('user_auth_sign', data_auth_sign($hasUser));
        session('aid',$aid);
        session('aid_title',$aid_title);
        session('shopid',$hasUser['shopid']);
        session('id',$hasUser['id']);
        session('name',$name);

        //更新管理员状态
        $param = [
            'loginnum' => $hasUser['loginnum'] + 1,
            'last_login_ip' => request()->ip(),
            'last_login_time' => time()
        ];

        db('admin_user')->where('id', $hasUser['id'])->update($param);

        //记录行为
        // action_log('user_login', 'Login', $hasUser['id'], $hasUser['id']);

        return json(['code' => 1, 'data' => url('index/index'), 'msg' => '登录成功']);
    }

    //验证码
    public function checkVerify()
    {
        $verify = new Verify();
        $verify->imageH = 32;
        $verify->imageW = 100;
        $verify->length = 4;
        $verify->useNoise = false;
        $verify->fontSize = 14;
        return $verify->entry();
    }

    //退出操作
    public function loginOut()
    {
        session('user_auth', null);
        session('id', null);
        session('aid', null);
        session('role', null);  //角色名
        session('rule', null);  //角色节点
        session('action', null);  //角色权限

        $this->redirect(url('index'));
    }
}